import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

typedef PickDoubleCallBack = Function(int index, int secondIndex);
typedef StringTransform = String Function<T>(T t);

//可以返回DateTime实体类替代index
class CustomPickDouble<T, V> extends StatelessWidget {
  const CustomPickDouble(
      {super.key,
      required this.firstList,
      required this.secondList,
      required this.confirmCallBack,
      this.cancelCallBack,
      this.firstStringTransform,
      this.secondStringTransform,
      this.firstIndex = 0,
      this.secondIndex = 0});

  //确认回调
  final PickDoubleCallBack confirmCallBack;

  //取消回调
  final PickDoubleCallBack? cancelCallBack;

  //数据list1
  final List<T> firstList;

  //数据list2
  final List<V> secondList;

  //string类型转换方法1
  final StringTransform? firstStringTransform;

  //string类型转换方法2
  final StringTransform? secondStringTransform;

  //索引1
  final int firstIndex;

  //索引2
  final int secondIndex;

  @override
  Widget build(BuildContext context) {
    FixedExtentScrollController firstScrollController =
        FixedExtentScrollController(initialItem: firstIndex);
    FixedExtentScrollController secondScrollController2 =
        FixedExtentScrollController(initialItem: secondIndex);
    int currentFirstIndex = firstIndex;
    int currentSecondIndex = secondIndex;
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(24), topRight: Radius.circular(24)),
      ),
      child: SizedBox(
        height: 600.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            //取消文本
            Positioned(
                left: 40.w,
                top: 24.h,
                child: InkWell(
                  onTap: () {
                    cancelCallBack?.call(
                            currentFirstIndex, currentSecondIndex) ??
                        Get.back();
                  },
                  child: Text(
                    "取消",
                    style: TextStyle(fontSize: 32.sp),
                  ),
                )),

            //确认文本
            Positioned(
                right: 40.w,
                top: 24.h,
                child: InkWell(
                  onTap: () {
                    confirmCallBack(currentFirstIndex, currentSecondIndex);
                    Get.back();
                  },
                  child: Text(
                    "确认",
                    style: TextStyle(fontSize: 32.sp),
                  ),
                )),

            //列表
            Positioned(
                top: 68.h,
                child: Row(
                  children: [
                    SizedBox(
                      height: 508.h,
                      width: 330.w,
                      child: CupertinoPicker(
                        itemExtent: 96.h,
                        diameterRatio: 1.0,
                        scrollController: firstScrollController,
                        selectionOverlay: _buildMagnifierScreen(),
                        onSelectedItemChanged: (int position) {
                          currentFirstIndex = position;
                        },
                        children: firstList
                            .map((data) =>
                                _customText(data, firstStringTransform))
                            .toList(),
                      ),
                    ),
                    SizedBox(
                      width: 1.w,
                      height: 508.h,
                      child: const DecoratedBox(
                        decoration: BoxDecoration(color: Color(0xFFD0D0D0)),
                      ),
                    ),
                    SizedBox(
                      height: 508.h,
                      width: 330.w,
                      child: CupertinoPicker(
                        itemExtent: 96.h,
                        diameterRatio: 1.0,
                        scrollController: secondScrollController2,
                        selectionOverlay: _buildMagnifierScreen(),
                        onSelectedItemChanged: (int position) {
                          currentSecondIndex = position;
                        },
                        children: secondList
                            .map((secondList) =>
                                _customVText(secondList, secondStringTransform))
                            .toList(),
                      ),
                    )
                  ],
                )),
          ],
        ),
      ),
    );
  }

  Widget _customText(T data, StringTransform? stringTransform) {
    String content = stringTransform?.call(data) ?? data.toString();
    return Container(
      alignment: Alignment.center,
      child: Text(
        content,
        style: TextStyle(fontSize: 36.sp, color: Colors.black),
      ),
    );
  }

  Widget _customVText(V data, StringTransform? stringTransform) {
    String content = stringTransform?.call(data) ?? data.toString();
    return Container(
      alignment: Alignment.center,
      child: Text(
        content,
        style: TextStyle(fontSize: 36.sp, color: Colors.black),
      ),
    );
  }

  Widget _buildMagnifierScreen() {
    const Color resolvedBorderColor = Color(0xFFD0D0D0);

    return IgnorePointer(
      child: Center(
        child: Container(
          decoration: const BoxDecoration(
            border: Border(
              top: BorderSide(width: 0.0, color: resolvedBorderColor),
              bottom: BorderSide(width: 0.0, color: resolvedBorderColor),
            ),
          ),
          constraints: BoxConstraints.expand(
            height: 96.h,
          ),
        ),
      ),
    );
  }
}
